<?php
ob_start();
@include('include/common.inc.php');
$pro = $_REQUEST["pro"];     // 区域
$city = $_REQUEST["city"];     // 区域
$typex = $_REQUEST['type'];
$price = $_POST['price'];
$num = $_POST['num'];
$address = $_POST['address'];
$hou_name = $_POST['hou_name'];

/************Search的cookie*********************/
if($pro!=''||$city!=''||$typex!='')
{
	if($_COOKIE['Cookie5']=='')
	{
		$cookie_id = md5(time().rand(0,1000000));
		setcookie("Cookie5", $cookie_id, time()+86400*100000); /* 有效期5天 */

	}else{
		$cookie_id = $_COOKIE['Cookie5'];
	}
	$c_time = date('Y-m-d');

	$sqli = "select * from $TBL_CACHE where p_id='$pro' and c_id='$city'  and  t_id='$typex' and  cookie_id='$cookie_id'";
	$result = $db->query($sqli);
	if(!$db->fetch_array($result))
	{
		$iinsert = "insert into $TBL_CACHE set p_id='$pro',c_id='$city',t_id='$typex',c_time='$c_time',cookie_id='$cookie_id'";
		$db->query($iinsert);
	}
	 /* 有效期5天 */
}
/*********************************/

if($city==''&$pro!='')
{
	$sel4 = "select * from $TBL_HOU_CATE where parent_id ='$pro'";   //導航
	$res4 =$db->query($sel4);
	while($rows = $db->fetch_array($res4))
	{
		if($cate_id!='')
		{
			$cate_id=$rows['cate_id'].','.$cate_id;
		}else{
			$cate_id=$rows['cate_id'];
		}
	}
	$cate_id = " cate_id in ( $cate_id ) ";
}else if($city!=''){
	$cate_id = "cate_id = $city";
}else{
	$cate_id = "cate_id != 0";
}
switch($price)
{
	case '':
		$price_s = "";
	break;
	case '5000':
		$price_s = " and price=5000 ";
	break;
	case '8000':
		$price_s = " and price>5000 and price<=8000 ";
	break;
	case '12000':
		$price_s = " and price>8000 and price<=12000 ";
	break;
	case '15000':
		$price_s = " and price>12000 and price<=15000 ";
	break;
	case '20000':
		$price_s = " and price>15000 and price<=20000 ";
	break;
	case '30000':
		$price_s = " and price>20000 and price<=30000 ";
	break;
	case '50000':
		$price_s = " and price>30000 and price<=50000 ";
	break;
	case '50001':
		$price_s = " and price>50000";
	break;
}

$search = " where address like '%$address%' and hou_name like '%$hou_name%' and num like '%$num%' and type like '%$typex%' and ".$cate_id.$price_s;

//and
$elevator = $_POST['elevator'];
$unit = $_POST['unit'];
$area = $_POST['area'];
$age = $_POST['age'];

if($elevator!='')
{
	$search = $search . " and elevator = '$elevator'";
}

if($unit!='')
{
	$search = $search . " and unit = '$unit' ";
}

//thrfou modify
if($area!='')
{
	//以前的
	/*
	if($area==50)
	{
		$search = $search . " and area < 50";
	}elseif($area==100){
		$search = $search . " and  50<= area <=100";
	}else{
		$search = $search . " and area >100 ";
	}
	*/
	//thrfou modify
	$areaArray = explode("-", $area);
	//数据库设计得时候,area设计成varchar(20)了,所以这里要转型
	//9<=cast(area as DECIMAL) and cast(area as DECIMAL)<=10;
	$search = $search . " and  ".$areaArray[0]."<=cast(area as DECIMAL) and cast(area as DECIMAL)<=".$areaArray[1];
}
//end thrfou


//thrfou add（分店处理逻辑）
$storeTF = $_POST['store'];
if($storeTF!='')
{
	$search = $search . " and store = '$storeTF'";
}
//end thrfou add
if($age!='')
{
	$search = $search . " and age = '$age'";
}

if($_GET['p']!='')
{
	$search = $_SESSION['S'];
}else{
	$_SESSION['S'] = $search;
}
//分店一起查出来,thrfou
if($storeTF!='')
{
	$query = "select hs.*,st.name as st_name from $TBL_HOUSE hs left join $TBL_STORE st on hs.store = st.id ".$search." and del=0 order by add_time desc";
}else{
	$query = "select * from $TBL_HOUSE ".$search." and del=0 order by add_time desc";
}
$cur_page = (int)$_GET["p"];   //获取当前页
$url_param = preg_replace("/(p=[0-9]+)&/", "", $_SERVER['QUERY_STRING']);
$pages = new Pager($query, $cur_page, 12, $url_param);
$page_string = $pages -> getPageString();
$result = $pages -> getRsPerPage();
$count = @mysql_num_rows($result);
while($rows = $db->fetch_array($result))
{
	if($rows['hou_type']==1)
	{
		$rows['titlex'] ='租金';
	}elseif($rows['hou_type']==2){
		$rows['titlex'] ='賣價';
	}else{
		$rows['titlex'] ='賣價';
	}
	$rows['ADDR']=FSubstr(str_replace("&nbsp;","",strip_tags($rows[address])),0,20);
	$rows['TIT']=FSubstr(str_replace("&nbsp;","",strip_tags($rows[title])),0,26);
	$row1[]=$rows;
}

if($count ==0)
{
	$note = "對不起,沒有您要搜尋的房屋！";
}
if(empty($page_string)) $page_string='抱歉!您搜尋的物件已關閉,請回首頁重新搜尋。';
@include('number.php');//验证码
$smarty->assign("row1",$row1);
$smarty->assign("page_string",$page_string);

//thrfou modify(修改中,第二点,要是选择了分店,那么就展示不同页面)
if($storeTF!='')
{
	$smarty->display('search-store.html');
}else{
	$smarty->display('search.html');
}

?>